import rolelist from '../../config/menu'
import limits from '../../config/roleper';

let showRoleData = async (ctx,next) => {
    let roleData = await ctx.db.querys(`select * from role`)
    roleData.map(item=>item.roperm='['+item.roperm+']')
    ctx.body = {code:1,msg:'',data:{roleData,rolelist,limits}}
}

//角色添加跳转时获取的相关信息
let rolethings = async (ctx,next) =>{
    let obj = {rolelist,limits}
    ctx.body = {code:1,msg:'请求成功',data:obj}
}
//角色添加
let roleAdd = async (ctx,next) => {
    let isOpr = await ctx.isJudge(ctx,'A');

    // 判断当前用户是否可以操作
    if(!isOpr){
        ctx.body = {code:0,msg:'您无权操作'}
        return
    }
    let {obj} = ctx.request.body;
    console.log(obj)
    let data=await ctx.db.querys(`select * from role`);
    data=data.findIndex(item=>item.role===obj.role);
    if(data !==-1) return ctx.body={code:0,msg:'该角色已存在'}; 
    obj.createtime = new Date().getTime()+'';
    obj.state='ok';
    obj.roperm = obj.roperm.slice(1,obj.roperm.length-1);
    let add = await ctx.db.insert('role',obj)
    if(!add.affectedRows){
        ctx.body = {code:0,msg:'操作失败，请重新再来'}
        return
    }
    ctx.body={code:1,msg:'操作成功'}
}
//角色编辑
let roleEdit = async (ctx,next) => {
    let isOpr = await ctx.isJudge(ctx,'E');

    // 判断当前用户是否可以操作
    if(!isOpr){
        ctx.body = {code:0,msg:'您无权操作'}
        return
    }
    let {obj} = ctx.request.body;
     obj.roperm = obj.roperm.slice(1,obj.roperm.length-1);
    let arr =  ctx.db.querys(`select * from role where roleid='${obj.roleid}'`);
    let obj1 = arr[0]
     obj.role =  obj.role?obj.role:obj1.role
     obj.viewpermissions = obj.viewpermissions?obj.viewpermissions:obj1.viewpermissions
     obj.roperm = obj.roperm?obj.roperm:obj1.roperm
    //  obj.statperm = obj.statperm?obj.statperm:obj1.statperm
     let edit = await ctx.db.querys(`update role set role='${obj.role}',viewpermissions='${obj.viewpermissions}',roperm='${obj.roperm}'  where roleid='${obj.roleid}' `)
     if(!edit.affectedRows){
        ctx.body = {code:0,msg:'操作失败，请重新再来'}
        return
    }
    ctx.body={code:1,msg:'编辑成功'}
}
//冻结;
let roleFreeze = async (ctx,next) => {
    let isOpr = await ctx.isJudge(ctx,'D');

    // 判断当前用户是否可以操作
    if(!isOpr){
        ctx.body = {code:0,msg:'您无权操作'}
        return
    }
    let {roleid} = ctx.request.body;
    let arr = await ctx.db.querys(`select * from user`)
    let brr = arr.filter(item=>item.roleid === roleid)
    if(brr.length){
        ctx.body = {code:0,msg:'此角色已被使用，请先解除该角色'}
        return
    }
    let crr = await ctx.db.querys(`select * from role where roleid='${roleid}'`)
    if(crr[0].state==='freeze'){
        crr = await ctx.db.querys(`update role set state='ok' where roleid='${roleid}'`)
        if(!crr.affectedRows) return ctx.body = {code:0,msg:'请求失败，请稍后再试'}
    }else{
        crr = await ctx.db.querys(`update role set state='freeze' where roleid='${roleid}'`)
        if(!crr.affectedRows) return ctx.body = {code:0,msg:'请求失败，请稍后再试'}
    }
    
    ctx.body={code:1,msg:'操作成功'}
}

//角色搜索
let roleSearch = async (ctx,next) => {
    let {obj} = ctx.request.body
    let arr = await ctx.db.querys(`select * from role`);
    if(obj.role){
        arr = arr.filter(item=>item.role.includes(obj.role))
    }
    if(obj.state){
        arr = arr.filter(item=>item.state===obj.state)
    }
    ctx.body={code:1,msg:'查询成功',data:arr}
}

//下拉选;
let getRole = async (ctx,next) =>{
    let arr = await ctx.db.querys(`select roleid,role from role`)
    ctx.body ={code:1,msg:'请求成功',data:arr}
}
export default {
    showRoleData,roleAdd,roleEdit,roleSearch,getRole,rolethings,roleFreeze
}